package com.yonyou.brigade.rtpnr.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.order_center.model.*;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/19.
 */
public class OrderConvert {

    private static Logger logger=LoggerFactory.getLogger(OrderConvert.class);

    public static List<BriFlightPassenger> getBriFlightPassenger(JSONObject json,String orderNo) {
        List<BriFlightPassenger> passengers=new ArrayList<>();
        if(json!=null) {
            JSONObject jsonContent=json.getJSONObject("OrderInfo");
            JSONArray passengerInfosArray=jsonContent.getJSONArray("passengerInfo");
            if(passengerInfosArray!=null&&passengerInfosArray.size()>0) {
                for(int i=0;i<passengerInfosArray.size();i++) {
                    JSONObject passengerJson=passengerInfosArray.getJSONObject(i);
                    BriFlightPassenger passenger=new BriFlightPassenger();
                    passenger.setCardNo(passengerJson.getString("CardNo"));
                    passenger.setInsMoney(passengerJson.getBigDecimal("InsMoney"));
                    passenger.setTicketStatus(passengerJson.getString("TicketStatus"));
                    passenger.setPsgTpPrice(passengerJson.getBigDecimal("PsgTPPrcie"));
                    passenger.setPsgName(passengerJson.getString("PsgName"));
                    passenger.setIsRuleOver(passengerJson.getString("IsRuleOver"));
                    passenger.setCostCenter(passengerJson.getString("CostCenter"));
                    passenger.setCardType(convertCardType(passengerJson.getString("CardType")));
                    passenger.setPsgRefundPrice(passengerJson.getBigDecimal("PsgRefundPrice"));
                    passenger.setIsAllowRefund(passengerJson.getString("IsAllowRefund"));
                    passenger.setInsCount(passengerJson.getInteger("InsCount"));
                    passenger.setPurchaseBank(passengerJson.getString("PurchaseBank"));
                    passenger.setInsName(passengerJson.getString("InsName"));
                    List rulesArray=passengerJson.getJSONArray("NOTravelRules");
                    if(rulesArray==null)
                    {
                        rulesArray=new ArrayList();
                    }
                    passenger.setNoTravelRules(rulesArray.toString());
                    passenger.setOAUserCode(passengerJson.getString("OAUserCode"));
                    passenger.setCardNo(passengerJson.getString("CardNo"));
                    passenger.setInsId(passengerJson.getString("InsId"));
                    passenger.setTpCounterFee(passengerJson.getBigDecimal("TPCounterFee"));
                    passenger.setCounterFee(passengerJson.getBigDecimal("CounterFee"));
                    passenger.setTicketNo(passengerJson.getString("TicketNO"));
                    passenger.setDepartmentName(passengerJson.getString("DepartmentName"));
                    passenger.setPsgType(passengerJson.getString("PsgType"));
                    passenger.setOrderNo(orderNo);
                    passengers.add(passenger);

                }
                return  passengers;
            }
        }
        return  null;
    }

    public static BriFlightOrderContact getBriFlightOrderContactByJSON(JSONObject json,String orderNo) {
        BriFlightOrderContact contact;
        if(json!=null) {
            JSONObject jsonContent=json.getJSONObject("OrderInfo");
            JSONObject personJson=jsonContent.getJSONObject("ContactPerson");
            contact=new BriFlightOrderContact();
            contact.setContactMobile(personJson.getString("ContactMobile"));
            contact.setContactName(personJson.getString("ContactName"));
            contact.setOrderNo(orderNo);
            return  contact;
        }
        return  null;
    }


    public static BriFlightOrder getBriFlightOrderByJSON(JSONObject json) {
        if(json!=null) {
            BriFlightOrder briFlightOrder=new BriFlightOrder();
            briFlightOrder.setOrderNo(json.getString("OrderNO"));
            briFlightOrder.setPublicOrPrivate(json.getString("PublicOrPriveate"));
            briFlightOrder.setIsDomc(json.getString("IsDomc"));
            briFlightOrder.setOrderStatus(json.getString("OrderStatus"));
            briFlightOrder.setOrderTime(json.getDate("OrderTime"));
            briFlightOrder.setPaymentPrice(json.getBigDecimal("PaymentPrice"));
            briFlightOrder.setStatusType(json.getString("StatusType"));
            briFlightOrder.setYxDate(json.getDate("YXDate"));
            briFlightOrder.setSavePrice(json.getBigDecimal("SavePrice"));
            briFlightOrder.setCreateBy(json.getString("CreateBy"));
            briFlightOrder.setPreiumReason(json.getString("PreiumReason"));
            briFlightOrder.setSysUpdateTime(json.getDate("SysUpdateTime"));
            return  briFlightOrder;
        }
        else {
            return  null;
        }
    }

    public static BriFlightOrder getBriFlightOrderDetailByJSON(JSONObject json) {
        if(json!=null) {
            JSONObject jsonContent = json.getJSONObject("OrderInfo");
            BriFlightOrder briFlightOrder=new BriFlightOrder();
            briFlightOrder.setOrderType(jsonContent.getString("Ordertype"));
            briFlightOrder.setIsAllowCheck(jsonContent.getString("IsAllowCheck"));
            briFlightOrder.setRefundPrice(jsonContent.getBigDecimal("RefundPrice"));
            briFlightOrder.setSumCounterFee(jsonContent.getBigDecimal("SumCounterFee"));
            briFlightOrder.setCreateBy(jsonContent.getString("CreateBy"));
            briFlightOrder.setOrderNo(jsonContent.getString("OrderNo"));
            briFlightOrder.setParentOrderNo(jsonContent.getString("ParentOrderNo"));
            briFlightOrder.setTpPrice(jsonContent.getBigDecimal("TPPrcie"));
            briFlightOrder.setVerifyType(jsonContent.getString("VerifyType"));
            briFlightOrder.setIsDomc(jsonContent.getString("IsDomc"));
            briFlightOrder.setOrderStatus(jsonContent.getString("OrderStatus"));
            briFlightOrder.setFlowBaseId(jsonContent.getString("FlowBaseID"));
            briFlightOrder.setPublicOrPrivate(jsonContent.getString("PublicOrPriveate"));
            briFlightOrder.setRefundMark(jsonContent.getString("RefundMark"));
            briFlightOrder.setSumInsMoney(jsonContent.getBigDecimal("SumInsMoney"));
            briFlightOrder.setRelatedNo(jsonContent.getString("RelatedNo"));
            briFlightOrder.setIsPurchase(jsonContent.getString("IsPurchase"));
            briFlightOrder.setPaymentPrice(jsonContent.getBigDecimal("PaymentPrice"));
            briFlightOrder.setIsCheck(jsonContent.getString("IsCheck"));
            briFlightOrder.setApplyOrderNo(jsonContent.getString("ApplyOrderNo"));
            briFlightOrder.setRelateYbzId(jsonContent.getString("ApplyOrderNo"));
            briFlightOrder.setIsAllowRefund(jsonContent.getString("IsAllowRefund"));
            briFlightOrder.setFarePrice(jsonContent.getBigDecimal("FarePrice"));
            briFlightOrder.setJsonOtherInfo(jsonContent.getString("JsonOtherInfo"));
            briFlightOrder.setPreiumReason(jsonContent.getString("PremiumReason"));
            briFlightOrder.setAllowPayDate(jsonContent.getDate("AllowPayDate"));
            briFlightOrder.setHsTitle(jsonContent.getString("HsTitle"));
            briFlightOrder.setHeadUserName(jsonContent.getString("HeadUserName"));
            briFlightOrder.setRelatedType(jsonContent.getString("RelatedType"));
            briFlightOrder.setPurchaseCustName(jsonContent.getString("PurchaseCustName"));
            briFlightOrder.setTripType(jsonContent.getString("TripType"));
            briFlightOrder.setIsHs(jsonContent.getString("IsHS"));
            briFlightOrder.setPsgCount(jsonContent.getInteger("PsgCount"));
            briFlightOrder.setPurchaseVerify(jsonContent.getString("PurchaseVerify"));
            briFlightOrder.setTaxPrice(jsonContent.getBigDecimal("TaxPrice"));
            //中航易购没有表明现付和预付的字段，只是通过paymentType来区分
//            briFlightOrder.setPayType(jsonContent.getString("PayType"));

            if ("XJ".equals(jsonContent.getString("PayType"))) {
                briFlightOrder.setPaymentType("CASH");
            } else {
                briFlightOrder.setPaymentType(jsonContent.getString("PayType"));
            }
            //根据支付方式判断支付类型
            if ("QK".equals(jsonContent.getString("PayType")) || "ZH".equals(jsonContent.getString("PayType")) || "XJ".equals(jsonContent.getString("PayType"))) {
                briFlightOrder.setPayType("Prepay");
                if ("D".equals(jsonContent.getString("IsDomc"))) {
                    briFlightOrder.setCategory("youshanglv_qiyezhifu");
                } else if ("I".equals(jsonContent.getString("IsDomc"))) {
                    briFlightOrder.setCategory("youshanglv_qiyezhifu_guoji");
                }
            } else {
                briFlightOrder.setPayType("Selfpay");
                if ("D".equals(jsonContent.getString("IsDomc"))) {
                    briFlightOrder.setCategory("youshanglv");
                } else if ("I".equals(jsonContent.getString("IsDomc"))) {
                    briFlightOrder.setCategory("youshanglv_guoji");
                }
            }
            briFlightOrder.setFromType("rtpnr");
            briFlightOrder.setChannelId(json.getString("channelId"));
            return briFlightOrder;
        }
        return  null;
    }

    public static List<BriFlightInfo> getBriFlightInfoByJSON(JSONObject json,String orderNO) {
        List<BriFlightInfo> briFlightInfos=new ArrayList<>();
        if(json!=null) {
            JSONObject jsonContent=json.getJSONObject("OrderInfo");
            JSONArray flightInfoArray=jsonContent.getJSONArray("FlightInfo");
            if(flightInfoArray!=null) {
                for (int i = 0; i < flightInfoArray.size(); i++) {
                    BriFlightInfo briFlightInfo = new BriFlightInfo();
                    JSONObject flightInfoJSON = flightInfoArray.getJSONObject(i);
                    briFlightInfo.setDepartureTime(DateUtils.strToTime(flightInfoJSON.getString("DepartureTime")));
                    briFlightInfo.setCarrierName(flightInfoJSON.getString("CarrierName"));
                    if(!StringUtils.isEmpty( flightInfoJSON.getString("ArriveTime")))
                    {

                        briFlightInfo.setArriveTime(DateUtils.strToTime(flightInfoJSON.getString("ArriveTime")));
                    }
                    else
                    {
                        logger.error(jsonContent.getString("OrderNo")+"没有到达时间信息");
                        briFlightInfo.setArriveTime(DateUtils.strToTime("23:59"));
                    }

                    briFlightInfo.setOffPoint(flightInfoJSON.getString("OffPoint"));
                    briFlightInfo.setOrderNo(orderNO);
                    briFlightInfo.setClassLevelName(flightInfoJSON.getString("ClassLevelName"));
                    briFlightInfo.setOffPointName(flightInfoJSON.getString("OffPointName"));
                    briFlightInfo.setOffPointAt(flightInfoJSON.getString("OffPointAT"));
                    briFlightInfo.setYprice(flightInfoJSON.getString("YPrice"));
                    briFlightInfo.setBoardPointAt(flightInfoJSON.getString("BoardPointAT"));
                    briFlightInfo.setFlightNo(flightInfoJSON.getString("FlightNO"));
                    briFlightInfo.setBoardPointAirPortName(flightInfoJSON.getString("BoardPointAirPortName"));
                    briFlightInfo.setBoardPointName(flightInfoJSON.getString("BoardPointName"));
                    briFlightInfo.setTimeSpan(flightInfoJSON.getString("TimeSpan"));
                    briFlightInfo.setClassCode(flightInfoJSON.getString("ClassLevelCode"));
                    String levelCode = flightInfoJSON.getString("ClassLevelCode");
                    if (!StringUtil.isNotEmpty(levelCode) && levelCode.startsWith("F")) {
                        briFlightInfo.setClassLevelCode("plane_01");
                        if (StringUtils.isEmpty(flightInfoJSON.getString("ClassLevelName"))) {
                            briFlightInfo.setClassLevelName("头等舱");
                        }
                    } else if (!StringUtil.isNotEmpty(levelCode) && (levelCode.startsWith("C") || levelCode.startsWith("B"))) {
                        briFlightInfo.setClassLevelCode("plane_02");
                        if (StringUtils.isEmpty(flightInfoJSON.getString("ClassLevelName"))) {
                            briFlightInfo.setClassLevelName("公务舱");
                        }
                    } else  {
                        briFlightInfo.setClassLevelCode("plane_03");
                        if (StringUtils.isEmpty(flightInfoJSON.getString("ClassLevelName"))) {
                            briFlightInfo.setClassLevelName("经济舱");
                        }
                    }
                    if (!StringUtils.isEmpty(flightInfoJSON.getString("ClassRebate"))) {
                        BigDecimal classRebate = new BigDecimal(flightInfoJSON.getString("ClassRebate"));
                        BigDecimal bigDecimal = new BigDecimal(1);
                        if (classRebate.compareTo(bigDecimal) == 1) {
                            classRebate = classRebate.setScale(2, BigDecimal.ROUND_HALF_UP);
                        } else if (classRebate.compareTo(bigDecimal) == -1) {
                            BigDecimal bigDecimal1 = new BigDecimal(100);
                            classRebate = classRebate.multiply(bigDecimal1).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        briFlightInfo.setClassRebate(String.valueOf(classRebate));
                    }
                    briFlightInfo.setBoardPoint(flightInfoJSON.getString("BoardPoint"));
                    briFlightInfo.setDepartureDate(flightInfoJSON.getDate("DepartureDate"));
                    briFlightInfo.setArriveDate(flightInfoJSON.getDate("ArriveDate"));
                    briFlightInfo.setAircraft(flightInfoJSON.getString("Aircraft"));
                    briFlightInfo.setOffPointAirPortName(flightInfoJSON.getString("OffPointAirPortName"));
                    briFlightInfo.setTgq(flightInfoJSON.getString("TGQ"));
                    briFlightInfo.setFromType(ChannelEnum.RTPNR.getName());
                    briFlightInfos.add(briFlightInfo);
                }
            }
        }
        return  briFlightInfos;
    }

    public static BriTrainOrder getBriTrainOrderDetailByJSON(JSONObject json) {
        if(json!=null) {
            JSONObject jsonContent = json.getJSONObject("OrderInfo");
            BriTrainOrder briTrainOrder = new BriTrainOrder();
            briTrainOrder.setLinkName(jsonContent.getString("LinkName"));
            briTrainOrder.setOrderType(jsonContent.getString("Ordertype"));
            briTrainOrder.setIsAllowCheck(jsonContent.getString("IsAllowCheck"));
            briTrainOrder.setOrderStatusName(jsonContent.getString("OrderStatusName"));
            briTrainOrder.setRefundPrice(jsonContent.getBigDecimal("RefundPrice"));
            briTrainOrder.setSumCounterFee(jsonContent.getBigDecimal("SumCounterFee"));
            briTrainOrder.setCheckStatus(jsonContent.getString("CheckStatus"));
            briTrainOrder.setCreateBy(jsonContent.getString("CreateBy"));
            briTrainOrder.setOrderNo(jsonContent.getString("OrderNo"));
            briTrainOrder.setTpPrice(jsonContent.getBigDecimal("TPPrcie"));
            briTrainOrder.setVerifyType(jsonContent.getString("VerifyType"));
            briTrainOrder.setParentOrderNo(jsonContent.getString("ParentOrderNo"));
            briTrainOrder.setPayLimitStatus(jsonContent.getString("PayLimitStatus"));
            briTrainOrder.setPayLimitTime(jsonContent.getDate("PayLimitTime"));
            briTrainOrder.setOrderStatus(jsonContent.getString("OrderStatus"));
            briTrainOrder.setFlowBaseId(jsonContent.getString("FlowBaseID"));
            briTrainOrder.setPublicOrPriveate(jsonContent.getString("PublicOrPriveate"));
            briTrainOrder.setPaymentPrice(jsonContent.getBigDecimal("PaymentPrice"));
            briTrainOrder.setIsCheck(jsonContent.getString("IsCheck"));
            briTrainOrder.setApplyOrderNo(jsonContent.getString("ApplyOrderNo"));
            briTrainOrder.setRefundSwiftNo(jsonContent.getString("RefundSwiftNo"));
            briTrainOrder.setJsonOtherInfo(jsonContent.getString("JsonOtherInfo"));
            briTrainOrder.setAllowPayDate(jsonContent.getDate("AllowPayDate"));
            briTrainOrder.setHeadUserName(jsonContent.getString("HeadUserName"));
            briTrainOrder.setCarrierSumFee(jsonContent.getBigDecimal("CarrierSumFee"));
            briTrainOrder.setIsSupplement(jsonContent.getInteger("ISSupplement"));
            briTrainOrder.setCancelReason(jsonContent.getString("CancelReason"));
            briTrainOrder.setLinkPhone(jsonContent.getString("LinkPhone"));
            briTrainOrder.setFromType(ChannelEnum.RTPNR.getName());
            briTrainOrder.setChannelId(json.getString("channelId"));
            if ("QK".equals(jsonContent.getString("PayType")) || "ZH".equals(jsonContent.getString("PayType")) || "XJ".equals(jsonContent.getString("PayType"))) {
                briTrainOrder.setPayType("Prepay");
                if ("D".equals(json.getString("IsDomc"))) {
                    briTrainOrder.setCategory("youshanglv_qiyezhifu");
                } else if ("I".equals(json.getString("IsDomc"))) {
                    briTrainOrder.setCategory("youshanglv_qiyezhifu_guoji");
                }
            } else {
                briTrainOrder.setPayType("Selfpay");
                if ("D".equals(json.getString("IsDomc"))) {
                    briTrainOrder.setCategory("youshanglv");
                } else if ("I".equals(json.getString("IsDomc"))) {
                    briTrainOrder.setCategory("youshanglv_guoji");
                }
            }
//            briTrainOrder.setTicketsNum(json.getJSONArray("TrainPsgInfo").size());
//            briTrainOrder.setTrainCodeName(jsonContent.getString("TrainCodeName"));
//            briTrainOrder.setToTime(DateUtils.strToTime(jsonContent.getString("ToTime")));
//            briTrainOrder.setSeatType(jsonContent.getString("SeatType"));
//            briTrainOrder.setFromTime(DateUtils.strToTime(jsonContent.getString("FromTime")));
//            briTrainOrder.setIsAllowRefund(jsonContent.getString("IsAllowRefund"));
//            briTrainOrder.setToStation(jsonContent.getString("ToStation"));
//            briTrainOrder.setTrainCode(jsonContent.getString("TrainCode"));
//            briTrainOrder.setFromStation(jsonContent.getString("FromStation"));
//            briTrainOrder.setTrainDate(jsonContent.getDate("TrainDate"));
//            briTrainOrder.setTrainOrderNo(jsonContent.getString("TrainOrderNo"));
//            briTrainOrder.setFromXq(jsonContent.getString("FromXQ"));
//            briTrainOrder.setTunTime(jsonContent.getString("TunTime"));
//            briTrainOrder.setToXq(jsonContent.getString("ToXQ"));
//            briTrainOrder.setToDate(jsonContent.getDate("ToDate"));
            return  briTrainOrder;
        }
        return  null;
    }

    public static BriTrainOrder getBriTrainOrderByJSON(JSONObject json) {
        if(json!=null) {
            BriTrainOrder briTrainOrder=new BriTrainOrder();
            briTrainOrder.setOrderNo(json.getString("OrderNO"));
            briTrainOrder.setPublicOrPriveate(json.getString("PublicOrPriveate"));
            briTrainOrder.setIsDomc(json.getString("IsDomc"));
            briTrainOrder.setOrderStatus(json.getString("OrderStatus"));
            briTrainOrder.setOrderTime(json.getDate("OrderTime"));
            briTrainOrder.setPaymentPrice(json.getBigDecimal("PaymentPrice"));
            briTrainOrder.setStatusType(json.getString("StatusType"));
            briTrainOrder.setYxDate(json.getDate("YXDate"));
            briTrainOrder.setSavePrice(json.getBigDecimal("SavePrice"));
            briTrainOrder.setCreateBy(json.getString("CreateBy"));
            briTrainOrder.setPreiumReason(json.getString("PreiumReason"));
            return  briTrainOrder;
        } else {
            return  null;
        }
    }


    public static List<BriTrainPassenger> getBriTrainPassngerByJSON(JSONObject json,String orderNo) {
        List<BriTrainPassenger> trainPassengers=new ArrayList<>();
        if(json != null) {
            JSONObject jsonContent=json.getJSONObject("OrderInfo");
            JSONArray passengerInfosArray=jsonContent.getJSONArray("TrainPsgInfo");
            if(passengerInfosArray != null && passengerInfosArray.size() > 0) {
                for (int i = 0; i < passengerInfosArray.size(); i++) {
                    JSONObject passengerJSON = passengerInfosArray.getJSONObject(i);
                    BriTrainPassenger briTrainPassenger=new BriTrainPassenger();
                    briTrainPassenger.setOrderNo(orderNo);
                    briTrainPassenger.setPsgStatus(passengerJSON.getString("PsgStatus"));
                    briTrainPassenger.setPsgName(passengerJSON.getString("PsgName"));
                    briTrainPassenger.setOAUserCode(passengerJSON.getString("OAUserCode"));
                    briTrainPassenger.setCardNo(passengerJSON.getString("CardNo"));
                    briTrainPassenger.setIsRuleOver(passengerJSON.getString("IsRuleOver"));
                    briTrainPassenger.setCostCenter(passengerJSON.getString("CostCenter"));
                    briTrainPassenger.setCardType(convertTrainCardType(passengerJSON.getString("CardType")));
                    briTrainPassenger.setUserId(passengerJSON.getString("UserId"));
                    briTrainPassenger.setPsgNo(passengerJSON.getString("PsgNO"));
                    briTrainPassenger.setPersonMobile(passengerJSON.getString("PersonMobile"));
                    briTrainPassenger.setTicketType(passengerJSON.getInteger("TicketType"));
                    briTrainPassenger.setNoTravelRules(passengerJSON.getString("NOTravelRules"));
                    briTrainPassenger.setIsCusPsg(passengerJSON.getString("IsCusPsg"));
//                    briTrainPassenger.setTktPrice(passengerJSON.getBigDecimal("TktPrice"));
//                    briTrainPassenger.setTrainBox(passengerJSON.getString("TrainBox"));
//                    briTrainPassenger.setRefundAmount(passengerJSON.getBigDecimal("RefundAmount"));
//                    briTrainPassenger.setTpPrice(passengerJSON.getBigDecimal("TPPrcie"));
//                    briTrainPassenger.setSeatType(passengerJSON.getString("SeatType"));
//                    briTrainPassenger.setCounterFee(passengerJSON.getBigDecimal("CounterFee"));
//                    briTrainPassenger.setSeatNo(passengerJSON.getString("SeatNo"));
//                    briTrainPassenger.setCarrierChangeFee(passengerJSON.getBigDecimal("CarrierChangeFee"));
//                    briTrainPassenger.setIsAllowRefund(passengerJSON.getString("IsAllowRefund"));
                    trainPassengers.add(briTrainPassenger);
                }
                return  trainPassengers;
            }
        }
        return  null;
    }

    public static List<BriTrainTicket> getBriTrainTicketByJSON(JSONObject trainDetailJson, String cardNo) {
        List<BriTrainTicket> tickets = new ArrayList<>();
        BriTrainTicket briTrainTicket = new BriTrainTicket();
        JSONObject jsonContent = trainDetailJson.getJSONObject("OrderInfo");
        briTrainTicket.setCardNo(cardNo);
        briTrainTicket.setOrderNo(jsonContent.getString("OrderNo"));
        briTrainTicket.setTrainCode(jsonContent.getString("TrainCode"));
        briTrainTicket.setTrainCodeName(jsonContent.getString("TrainCodeName"));
        briTrainTicket.setFromStation(jsonContent.getString("FromStation"));
        briTrainTicket.setToStation(jsonContent.getString("ToStation"));
        briTrainTicket.setTrainDate(jsonContent.getDate("TrainDate"));
        briTrainTicket.setFromTime(DateUtils.strToTime(jsonContent.getString("FromTime")));
        briTrainTicket.setToDate(jsonContent.getDate("ToDate"));
        briTrainTicket.setToTime(DateUtils.strToTime(jsonContent.getString("ToTime")));
        briTrainTicket.setTunTime(jsonContent.getString("TunTime"));
        briTrainTicket.setTrainOrderNo(jsonContent.getString("TrainOrderNo"));
        JSONArray trainPsgInfos = jsonContent.getJSONArray("TrainPsgInfo");
        if (trainPsgInfos != null && trainPsgInfos.size() > 0) {
            for (int i = 0; i < trainPsgInfos.size(); i++) {
                JSONObject trainPsgInfosJSONObject = trainPsgInfos.getJSONObject(i);
                if (!StringUtils.isEmpty(cardNo) && cardNo.equals(trainPsgInfosJSONObject.getString("CardNo"))) {
                    briTrainTicket.setTrainBox(trainPsgInfosJSONObject.getString("TrainBox"));
                    briTrainTicket.setSeatNo(trainPsgInfosJSONObject.getString("SeatNo"));
                    briTrainTicket.setRefundAmount(trainPsgInfosJSONObject.getBigDecimal("RefundAmount"));
                    briTrainTicket.setTpPrice(trainPsgInfosJSONObject.getBigDecimal("TPPrcie"));
                    briTrainTicket.setCounterFee(trainPsgInfosJSONObject.getBigDecimal("CounterFee"));
                    briTrainTicket.setCarrierChangeFee(trainPsgInfosJSONObject.getBigDecimal("CarrierChangeFee"));
                    briTrainTicket.setIsAllowRefund(trainPsgInfosJSONObject.getString("IsAllowRefund"));
                    briTrainTicket.setSeatType(trainPsgInfosJSONObject.getString("SeatType"));
                    briTrainTicket.setTktPrice(trainPsgInfosJSONObject.getBigDecimal("TktPrice"));
                    tickets.add(briTrainTicket);
                }
            }
            return tickets;
        }
        tickets.add(briTrainTicket);
        return tickets;
    }

//    public static BriTrainPayment getBriTrainPaymentByJSON(JSONObject trainDetailJson, String orderNo) {
//        BriTrainPayment briTrainPayment = new BriTrainPayment();
//        JSONObject orderInfo = trainDetailJson.getJSONObject("OrderInfo");
//        briTrainPayment.setOrderNo(orderNo);
//        briTrainPayment.setPayWayId(orderInfo.getString(""));
//        return briTrainPayment;
//    }

    public static List<BriTrainApprovalFlow> getBriTrainApprovalFlowByJSON(JSONObject json,String orderNo) {
        List<BriTrainApprovalFlow> trainApprovalFlows=new ArrayList<>();
        if(json != null) {
            JSONObject jsonContent=json.getJSONObject("OrderInfo");
            if (jsonContent != null) {
                JSONArray approvalFlowsArray = jsonContent.getJSONArray("ApprovalFlowStepList");
                if (approvalFlowsArray != null && approvalFlowsArray.size() > 0) {
                    for (int i = 0; i < approvalFlowsArray.size(); i++) {
                        JSONObject approvalFlowJSON = approvalFlowsArray.getJSONObject(i);
                        BriTrainApprovalFlow briTrainApprovalFlow = new BriTrainApprovalFlow();
                        briTrainApprovalFlow.setOrderNO(orderNo);
                        briTrainApprovalFlow.setApprovalNo(approvalFlowJSON.getString("ApprovalNo"));
                        briTrainApprovalFlow.setStepNO(approvalFlowJSON.getString("StepNO"));
                        briTrainApprovalFlow.setStepRunStatus(approvalFlowJSON.getString("StepRunStatus"));
                        briTrainApprovalFlow.setDealerUserName(approvalFlowJSON.getString("DealerUserName"));
                        briTrainApprovalFlow.setCheckRemark(approvalFlowJSON.getString("CheckRemark"));
                        briTrainApprovalFlow.setCheckTime(approvalFlowJSON.getDate("CheckTime"));
                        trainApprovalFlows.add(briTrainApprovalFlow);
                    }
                    return trainApprovalFlows;
                }
            }
        }
        return  null;
    }


    public static BriHotelOrder getBriHotelOrderByJSON(JSONObject json) {
        if(json != null) {
            BriHotelOrder briHotelOrder=new BriHotelOrder();
            briHotelOrder.setOrderNo(json.getString("OrderNO"));
            briHotelOrder.setPublicOrPrivate(json.getString("PublicOrPriveate"));
            briHotelOrder.setIsDomc(json.getString("IsDomc"));
            briHotelOrder.setOrderStatus(json.getString("OrderStatus"));
            briHotelOrder.setOrderTime(json.getDate("OrderTime"));
            briHotelOrder.setPaymentPrice(json.getBigDecimal("PaymentPrice"));
            briHotelOrder.setStatusType(json.getString("StatusType"));
            briHotelOrder.setYxDate(json.getDate("YXDate"));
            briHotelOrder.setSavePrice(json.getBigDecimal("SavePrice"));
            briHotelOrder.setCreateBy(json.getString("CreateBy"));
            briHotelOrder.setPreiumReason(json.getString("PreiumReason"));

            return  briHotelOrder;
        } else {
            return  null;
        }
    }

    public static BriHotelOrder getBriHotelOrderDetailByJSON(JSONObject hotelDetailJson) {
        if(hotelDetailJson != null) {
            JSONObject jsonContent = hotelDetailJson.getJSONObject("OrderInfo");
            BriHotelOrder briHotelOrder = new BriHotelOrder();
            briHotelOrder.setOrderNo(jsonContent.getString("OrderNO"));
            briHotelOrder.setTravelType(jsonContent.getString("TravelType"));
            briHotelOrder.setIsDomc(jsonContent.getString("IsDomc"));
            briHotelOrder.setOrderStatus(jsonContent.getString("OrderStatus"));
            briHotelOrder.setOrderStatusName(jsonContent.getString("OrderStatusName"));
            briHotelOrder.setPaymentPrice(jsonContent.getBigDecimal("PaymentPrice"));
            briHotelOrder.setHotelName(jsonContent.getString("HotelName"));
            briHotelOrder.setHotelId(jsonContent.getString("HotelID"));
            briHotelOrder.setRoomName(jsonContent.getString("RoomName"));
            briHotelOrder.setRoomTypeId(jsonContent.getString("RoomTypeId"));
            briHotelOrder.setRatePlanName(jsonContent.getString("RatePlanName"));
            briHotelOrder.setRatePlanId(jsonContent.getString("RatePlanId"));
            briHotelOrder.setRoomNums(jsonContent.getInteger("RoomNums"));
            briHotelOrder.setArrivalDate(jsonContent.getDate("ArrivalDate"));
            briHotelOrder.setDepartureDate(jsonContent.getDate("DepartureDate"));
            briHotelOrder.setInDays(jsonContent.getInteger("InDays"));
            briHotelOrder.setLatestArrivalTime(DateUtils.strToDateTime(jsonContent.getString("LatestArrivalTime")));
            briHotelOrder.setHotelAddress(jsonContent.getString("HotelAddress"));
            briHotelOrder.setHotelTel(jsonContent.getString("HotelTel"));
            briHotelOrder.setCreateTime(jsonContent.getDate("CreateTime"));
            briHotelOrder.setzPayType(jsonContent.getString("ZPayType"));
            if ("Prepay".equals(jsonContent.getString("PayType"))) {
                briHotelOrder.setPayType("Prepay");
                if ("D".equals(jsonContent.getString("IsDomc"))) {
                    briHotelOrder.setCategory("youshanglv_qiyezhifu");
                } else if ("I".equals(jsonContent.getString("IsDomc"))) {
                    briHotelOrder.setCategory("youshanglv_qiyezhifu_guoji");
                }
            } else {
                briHotelOrder.setPayType("Selfpay");
                if ("D".equals(jsonContent.getString("IsDomc"))) {
                    briHotelOrder.setCategory("youshanglv");
                } else if ("I".equals(jsonContent.getString("IsDomc"))) {
                    briHotelOrder.setCategory("youshanglv_guoji");
                }
            }
            briHotelOrder.setIsGuarantee(jsonContent.getInteger("IsGuarantee"));
            briHotelOrder.setGuaranteeInfo(jsonContent.getString("GuaranteeInfo"));
            briHotelOrder.setContactName(jsonContent.getString("ContactName"));
            briHotelOrder.setContactMobile(jsonContent.getString("ContactMobile"));
            briHotelOrder.setIsAllowCancel(jsonContent.getInteger("IsAllowCancel"));
            briHotelOrder.setCancelTime(jsonContent.getDate("CancelTime"));
            briHotelOrder.setGcjLat(jsonContent.getBigDecimal("GCJLat"));
            briHotelOrder.setGcjLon(jsonContent.getBigDecimal("GCJLon"));
            briHotelOrder.setBaiduLat(jsonContent.getBigDecimal("Baidulat"));
            briHotelOrder.setBaiduLon(jsonContent.getBigDecimal("Baidulon"));
            briHotelOrder.setVerifyType(jsonContent.getString("VerifyType"));
            briHotelOrder.setHeadUserName(jsonContent.getString("HeadUserName"));
            briHotelOrder.setFlowBaseId(jsonContent.getString("FlowBaseID"));
            briHotelOrder.setIsAllowCheck(jsonContent.getString("IsAllowCheck"));
            briHotelOrder.setPenaltyToCustomer(jsonContent.getString("PenaltyToCustomer"));
            briHotelOrder.setPenaltyCurrencyCode(jsonContent.getString("PenaltyCurrencyCode"));
            briHotelOrder.setPrepayRuleInfo(jsonContent.getString("PrepayRuleInfo"));
            briHotelOrder.setPassengerType(jsonContent.getString("PassengerType"));
//            briHotelOrder.setPaymentType(jsonContent.getString("ZPayType"));
            if ("XJ".equals(jsonContent.getString("ZPayType"))) {
                briHotelOrder.setPaymentType("CASH");
            } else {
                briHotelOrder.setPaymentType(jsonContent.getString("ZPayType"));
            }
            briHotelOrder.setInvoiceMode(jsonContent.getString("InvoiceMode"));
            briHotelOrder.setSumServiceCharge(jsonContent.getBigDecimal("SumServiceCharge"));
            briHotelOrder.setApplyOrderNo(jsonContent.getString("ApplyOrderNo"));
            briHotelOrder.setFromType(ChannelEnum.RTPNR.getName());
            briHotelOrder.setChannelId(hotelDetailJson.getString("channelId"));
            briHotelOrder.setHotelCity(jsonContent.getString("CityName"));
            if ("RMB".equals(jsonContent.getString("CurrencyCode"))) {
                briHotelOrder.setCurrency("CNY");
            } else {
                briHotelOrder.setCurrency(jsonContent.getString("CurrencyCode"));
            }
            return briHotelOrder;
        } else {
            return null;
        }
    }

    public static List<BriHotelRoom> getBriHotelRoomByJSON(JSONObject json,String orderNO) {
        List<BriHotelRoom> briHotelRooms = new ArrayList<>();
        if (json != null) {
            JSONObject jsonContent = json.getJSONObject("OrderInfo");
            if (jsonContent != null) {
                JSONArray roomPsgInfoArray = jsonContent.getJSONArray("RoomPsgInfo");
                if (roomPsgInfoArray != null && roomPsgInfoArray.size() > 0) {
                    for (int i = 0; i < roomPsgInfoArray.size(); i++) {
                        JSONObject roomInfoJSON = roomPsgInfoArray.getJSONObject(i);
                        JSONObject roomInfoJSONs = roomInfoJSON.getJSONObject("RoomInfo");
                        BriHotelRoom briHotelRoom = new BriHotelRoom();
                        briHotelRoom.setOrderNO(orderNO);
                        briHotelRoom.setPsgName(roomInfoJSONs.getString("PsgName"));
                        briHotelRoom.setOAUserCode(roomInfoJSONs.getString("OAUserCode"));
                        briHotelRoom.setPersonMobile(roomInfoJSONs.getString("PersonMobile"));
                        briHotelRoom.setCostCenter(roomInfoJSONs.getString("CostCenter"));
                        briHotelRoom.setIsRuleOver(roomInfoJSONs.getString("IsRuleOver"));
                        briHotelRoom.setNoTravelRules(roomInfoJSONs.getString("NOTravelRules"));
                        briHotelRoom.setServiceFee(roomInfoJSONs.getBigDecimal("ServiceFee"));
                        briHotelRooms.add(briHotelRoom);
                    }
                }
            }
            return briHotelRooms;
        }
        return null;
    }

    public static List<BriHotelApprovalFlow> getBriHotelApprovalFlowByJSON(JSONObject json,String orderNO) {
        List<BriHotelApprovalFlow> briHotelApprovalFlows = new ArrayList<>();
        if (json != null) {
            JSONObject jsonContent = json.getJSONObject("OrderInfo");
            if (jsonContent != null) {
                JSONArray approvalFlowStepListArray = jsonContent.getJSONArray("ApprovalFlowStepList");
                if (approvalFlowStepListArray != null && approvalFlowStepListArray.size() > 0) {
                    for (int i = 0; i < approvalFlowStepListArray.size(); i++) {
                        BriHotelApprovalFlow briHotelApprovalFlow = new BriHotelApprovalFlow();
                        JSONObject approvalFlowStepListJson = approvalFlowStepListArray.getJSONObject(i);
                        briHotelApprovalFlow.setOrderNO(orderNO);
                        briHotelApprovalFlow.setApprovalNo(approvalFlowStepListJson.getString("ApprovalNo"));
                        briHotelApprovalFlow.setStepNO(approvalFlowStepListJson.getString("StepNO"));
                        briHotelApprovalFlow.setStepRunStatus(approvalFlowStepListJson.getString("StepRunStatus"));
                        briHotelApprovalFlow.setDealerUserName(approvalFlowStepListJson.getString("DealerUserName"));
                        briHotelApprovalFlow.setCheckRemark(approvalFlowStepListJson.getString("CheckRemark"));
                        briHotelApprovalFlow.setCheckTime(approvalFlowStepListJson.getDate("CheckTime"));
                        briHotelApprovalFlows.add(briHotelApprovalFlow);
                    }
                }
            }
            return briHotelApprovalFlows;
        }
        return null;
    }

    public static String convertTrainCardType(String cardType) {
        String result = cardType;
        switch (cardType) {
            case "2":
                result = "二代身份证";
                break;
            case "3":
                result = "港澳通行证";
                break;
            case "4":
                result = "台湾通行证";
                break;
            case "5":
                result = "护照";
                break;
            default:
                break;
        }
        return result;
    }

    public static String convertCardType(String cardType) {
        String result = cardType;
        switch (cardType) {
            case "NI":
                result = "身份证";
                break;
            case "FOID":
                result = "护照";
                break;
            case "ID":
                result = "其他";
                break;
            default:
                break;
        }
        return result;
    }
}
